#include "datadeal.h"



rt_thread_t Mfrc_Thread;//uid卡读取线程
rt_thread_t CardDeal_Thread;//uid处理线程
rt_thread_t SSD1306_Thread;//OLED显示线程

rt_mailbox_t Uid_Mb;//uid发送邮箱
rt_mailbox_t Result_Mb;//判断是否为非法卡

rt_timer_t Led_Timer;//LED
rt_timer_t SSD1306_Timer;//OLED



int main(void)
{
    uint32_t Er_Card;
    uint8_t pin_r = rt_pin_get(LED_R);
    uint8_t pin_g = rt_pin_get(LED_G);
    uint8_t pin_b = rt_pin_get(LED_B);

    rt_pin_mode(pin_r, PIN_MODE_OUTPUT);
    rt_pin_mode(pin_g, PIN_MODE_OUTPUT);
    rt_pin_mode(pin_b, PIN_MODE_OUTPUT);

    Uid_Mb = rt_mb_create("Uid_Mb", 4, RT_IPC_FLAG_FIFO);
    Result_Mb = rt_mb_create("Result_Mb", 4, RT_IPC_FLAG_FIFO);
    Led_Timer = rt_timer_create("Led_Timer", timeout_callback, (void*)1, 3000, RT_TIMER_FLAG_ONE_SHOT);
    SSD1306_Timer = rt_timer_create("SSD1306_Timer", timeout_callback, (void*)2, 3000, RT_TIMER_FLAG_ONE_SHOT);
    Mfrc_Thread = rt_thread_create("Mfrc_Thread", Mfrc_Thread_Entry, RT_NULL, 1024, 9, 20);//uid读取线程
    if(Mfrc_Thread != RT_NULL)  rt_thread_startup(Mfrc_Thread);
    CardDeal_Thread = rt_thread_create("CardDeal_Thread", CardDeal_Thread_Entry, RT_NULL, 4096, 8, 20);//uid处理线程
    if(CardDeal_Thread != RT_NULL)  rt_thread_startup(CardDeal_Thread);
    SSD1306_Thread = rt_thread_create("SSD1306_Thread", SSD1306_Thread_Entry, RT_NULL, 2048, 7, 20);//OLED显示线程
    if(SSD1306_Thread != RT_NULL) rt_thread_startup(SSD1306_Thread);

    rt_pin_write(pin_r, OFF);
    rt_pin_write(pin_g, OFF);
    rt_pin_write(pin_b, OFF);

    while (1)
    {
        if(rt_mb_recv(Result_Mb, &Er_Card, RT_WAITING_FOREVER) == RT_EOK)
        {
            if(Er_Card == 1)
            {
                rt_pin_write(pin_r, ON);
                rt_pin_write(pin_g, OFF);
                rt_pin_write(pin_b, OFF);
                rt_timer_start(Led_Timer);
            }
            else {
                rt_pin_write(pin_r, OFF);
                rt_pin_write(pin_g, ON);
                rt_pin_write(pin_b, OFF);
                rt_timer_start(Led_Timer);
            }
        }

        rt_thread_mdelay(1000);
    }

    return 0;
}

